package com.example.springbootfileupload.test;


import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
 * @author by jingwei
 * @version V1.0
 * @classname FileUploader
 * @description mini-io方式
 * @date 2024/3/14 18:14
 */
public class FileUploader {
    public static void main(String[] args)
            throws IOException, NoSuchAlgorithmException, InvalidKeyException {
        try {
            // Create a minioClient with the MinIO server playground, its access key and secret key.
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint("http://127.0.0.1:9000")
                            .credentials("36DfMpMA4ADzXS5D1XXe", "sqcNbnQoiY47HtZe5VNujKYH8Poj0S3g0FaZtqeZ")
                            .build();

            // Make 'asiatrip' bucket if not exist.
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket("hkgyl-private-bulk").build());
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("hkgyl-private-bulk").build());
            } else {
                System.out.println("Bucket 'asiatrip' already exists.");
            }

            // Upload '/home/user/Photos/asiaphotos.zip' as object name 'asiaphotos-2015.zip' to bucket
            // 'asiatrip'.
            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket("hkgyl-private-bulk")
                            .object("test.txt")
                            .filename("E:\\project\\mine-project\\spring-series\\spring-boot\\springboot-miniio\\src\\main\\resources\\test.txt")
                            .build());
            System.out.println(
                    "'test.txt' is successfully uploaded as "
                            + "object 'test.txt' to bucket 'hkgyl-private-bulk'.");
        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
            System.out.println("HTTP trace: " + e.httpTrace());
        }
    }
}
